<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <span>ip:</span>&nbsp;
        <el-input
          clearable
          style="width: 200px"
          v-model="where.ip"
          placeholder="请输入对应ip"/>
        &nbsp;&nbsp;
        <span>访问时间:</span>&nbsp;
        <el-date-picker
          v-model="where.time"
          type="datetimerange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          :default-time="['00:00:00']">
        </el-date-picker>
        &nbsp;&nbsp;
        <span>国家:</span>&nbsp;
        <el-select class="form_width" filterable clearable v-model="where.country">
          <el-option v-for="item in visitorsList.country_list" :key="item" :label="item"
                     :value="item"/>
        </el-select>&nbsp;&nbsp;

        <span>访客类型:</span>&nbsp;
        <el-select class="form_width" filterable v-model="where.ip_type">
          <el-option v-for="(item,index) in visitorsList.ip_type_list" :key="parseInt(index)" :label="item"
                     :value="parseInt(index)"/>
        </el-select>&nbsp;&nbsp;
        <br/>
        <br/>
        <span>域名:</span>&nbsp;
<!--        <el-select class="form_width" clearable v-model="where.referrer">-->
<!--          <el-option v-for="item in visitorsList.referrer_list" :key="item" :label="item"-->
<!--                     :value="item"/>-->
<!--        </el-select>&nbsp;&nbsp;-->
        <el-input
          clearable
          style="width: 200px"
          v-model="where.referrer"
          placeholder="请输入域名关键词"/>
        <el-button
          type="primary"
          icon="el-icon-search"
          class="ele-btn-icon"
          @click="reload(1)">查询
        </el-button>
        <el-button @click="reset">重置</el-button>
        <br/><br/>

      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :selection.sync="selection"
        height="calc(100vh - 355px)">

        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button
            @click="exportData"
            type="primary"
            icon="el-icon-download"
            class="ele-btn-icon"
            size="small">导出
          </el-button>
        </template>

        <!-- 投放终端列 -->
        <template slot="is_mobile" slot-scope="{row}">
          <div v-if="row.is_mobile ===1">移动端</div>
          <div v-if="row.is_mobile ===2">PC端</div>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import XLSX from "xlsx";

export default {
  name: 'visitors',
  components: {},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/visitors/index',
      // 表格列配置
      columns: [
        // {
        //   prop: 'id',
        //   label: 'ID',
        //   width: 60,
        //   align: 'center',
        //   showOverflowTooltip: true,
        //   fixed: "left"
        // },
        {
          prop: 'ip',
          label: 'ip',
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'country',
          label: '国家',
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'province',
          label: '省份',
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'city',
          label: '城市',
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'area',
          label: '地区',
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'visitors_platform',
          label: '平台',
          align: 'center',
          minWidth: 100,
        },
        {
          prop: 'browser',
          label: '浏览器',
          align: 'center',
          minWidth: 100,
        },
        {
          prop: 'is_mobile',
          label: '访问设备',
          align: 'center',
          minWidth: 100,
          slot: 'is_mobile'
        },
        {
          prop: 'referrer',
          label: '访问域名',
          align: 'center',
          showOverflowTooltip: true,
          minWidth: 300,
        },
        {
          prop: 'create_time',
          label: '创建时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: 'update_time',
          label: '更新时间',
          sortable: 'custom',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center',
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
      ],
      // 表格搜索条件
      where: {
        ip_type:1,
        page:0,
        limit:10,
      },
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      // 栏目列表
      cateList: [],
      //搜索条件配置项
      visitorsList:[],
    };
  },
  mounted() {
    // 查询栏目列表
    // this.getCateList();
    this.getVisitorConfig();
  },
  methods: {
    /* 刷新表格 */
    reload(pageSign) {
      if (pageSign) this.where.page=1;
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    /* 导出数据 */
    exportData() {

      var ass = this.where;
      ass.page=1;
      ass.limit=50000;

      let array = [['ip', '国家', '平台', '浏览器', '访问设备', '访问域名', '创建时间']];
      const loading = this.$loading({lock: true});
      this.$http.get(this.url,{
        params:ass
      }).then(res => {
        loading.close();
        if (res.data.code === 0) {
          res.data.data.forEach(d => {
            array.push([
              d.ip,
              d.country,
              d.visitors_platform,
              d.browser,
              ['移动端','PC端'][d.is_mobile - 1],
              d.referrer,
              this.$util.toDateString(d.create_time)
            ]);
          });
          this.$util.exportSheet(XLSX, array, '访客日志');
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    getVisitorConfig()
    {
      this.$http.get('/visitors/getVisitorConfig').then(res => {
        if (res.data.code === 0) {
          this.visitorsList =res.data.data;
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.post('/visitors/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的广告位吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/visitors/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
    /**
     * 查询栏目列表
     */
    getCateList() {
      this.$http.get('/itemcate/getCateList').then(res => {
        if (res.data.code === 0) {
          this.cateList = this.$util.toTreeData(res.data.data, 'id', 'pid');
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },
  }
}
</script>

<style scoped>
</style>
